using MediatR;
using S.O.Api.Write.Application.Command.Role;
using S.O.Domain.RBAC;
using S.O.ErrorCode;
using S.O.Interstructrue.Base;

namespace S.O.Api.Write.Application.Handler.Role
{
    public class UpdateRoleEnableHandler : IRequestHandler<UpdateRoleEnableCommand, APIResult<int>>
    {
        private readonly IBaseRepository<RoleModel> roleRepository;
        private readonly ILogger<UpdateRoleEnableHandler> logger;

        public UpdateRoleEnableHandler(IBaseRepository<RoleModel> roleRepository, ILogger<UpdateRoleEnableHandler> logger)
        {
            this.roleRepository = roleRepository;
            this.logger = logger;
        }
        /// <summary>
        /// �޸Ľ�ɫ״̬
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<APIResult<int>> Handle(UpdateRoleEnableCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> res = new APIResult<int>();
            var model = roleRepository.GetALL().FirstOrDefault(x => x.Id == request.Id);
            model.IsEnable = request.IsEnable;
            var r = roleRepository.Update(model);
            if (r > 0)
            {
                res.Data = r;
                res.Code = APIEnums.Success;
                res.Msg = "�޸ĳɹ�";
                logger.LogInformation("�޸ĳɹ�");
                return Task.FromResult(res);
            }
            else
            {
                res.Code = APIEnums.Fail;
                res.Msg = "�޸�ʧ��";
                logger.LogError("�޸�ʧ��");
                return Task.FromResult(res);
            }
        }
    }
}
